<?php
/**
 * @file functions.php
 * @brief provides a list of general functions that can be used by all files that has included this file
 */
/**
check if a string starts with $prefix

@param $string
	The target string

@param $prefix
	string to be checked if the target string has as a start-with

@return 
	true if string starts with $prefix
*/
function starts_with($string, $prefix){
    // returns a bool
    return strpos($string, $prefix) === 0;
}

/**
check if an email is in valid format
@return 
	true - email format valid
	false - email format not valid

*/
function validateEmail($email){
	return filter_var($email, FILTER_VALIDATE_EMAIL);
}


/**
convert a number to 2 decimal places

@param $number
	the number to be converted.

*/
function toMoneyFormat($number){
	return number_format((float)$number,2);
}
 
/**
 * 随机生成一个 n 位string
 * @param $n
 * 	length of random string
 * @return String
 * 	random n digits string
 * 	
 */
function generateKey($length = 10){
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}

/**
js_redirect: 用js方式跳转页面
@param
	$msg 显示提示语
	$url 跳转至url
*/
function js_redirect($msg,$url,$delay=200){
	echo $msg.'<br />如果浏览器未自动跳转，请点击<a href="'.$url.'">这里</a>';
	echo '<script type="text/javascript">
			setTimeout(function () {
				window.location="'.$url.'"
			}, '.$delay.'); 
		</script>';
}
/**
 * 利用php mail function发送email 
 !!	to be deprecated and replace by using PHPMailer library
 * @param $to
 * 	收件人邮箱地址
 * @param $subject
 * 	主题
 * @param $message
 * 	内容
 * 
 * @return 
 * 	true 邮件送出，false 错误
 */
function sendEmail($recipient, $recipient_name, $from, $from_name, $subject, $message, $attachments=null,$is_important=false){
	require(ROOT_DIR.'libs/phpmailer/class.phpmailer.php');
	$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
	$mail->IsSendmail(); // telling the class to use SendMail transport
	
	try {
		
	  //$mail->AddReplyTo('name@yourdomain.com', 'First Last');
	  $mail->AddAddress($recipient, $recipient_name);
	  $mail->SetFrom($from, $from_name);
	  if($is_important){
		  $mail->Priority = 1;
		  $mail->addCustomHeader("X-Priority: 1");
		  $mail->addCustomHeader("X-MSMail-Priority: High");
		  $mail->addCustomHeader("Importance: High");
	  }
	  $mail->Subject = $subject;	  
	  $mail->MsgHTML($message);
	  if($attachments != null){
		  foreach($attachments as $attachment){
			  $mail->AddAttachment($attachment);      // attachment
		  }
	  }
	  $mail->Send();
	  
	  return true;
	} catch (phpmailerException $e) {
	  echo $e->errorMessage(); //Pretty error messages from PHPMailer
	  return false;
	} catch (Exception $e) {
	  echo $e->getMessage(); //Boring error messages from anything else!
	  return false;
	}
}
/**
 * 
 */
function encrypt($string){
	return md5($string);
}

/**
 * 输出存在session里的消息并unset该消息变量
 * @param $key
 *  String type, list of keys accessible: 
 * 	login_error, reg_error, 
 */
function printSession($key){
	if(isset($key)){
		echo '<div class="alert">';
		echo '<button type="button" class="close" data-dismiss="alert">&times;</button>';
		echo $_SESSION[$key];
		echo '</div>';
		unset($_SESSION[$key]);		
	}
}	

/**
 * set session variable
 */
function setSession($key,$value){
	$_SESSION[$key]=$value;
} 
/**
 * get session variable
 */
function getSession($key){
	return $_SESSION[$key];
}

function postvar($key){
	return strip_tags(trim($_POST[$key]));
}

function loadJS($src){
	echo '<script type="text/javascript" src="'.$src.'"></script>';
}
function writeScript($script){
	echo '<script type="text/javascript">';
	echo $script;
	echo '</script>';
}

/**
	output a line of text with the <br /> tag
*/
function writeln($msg=''){
	echo $msg.'<br />';
}


?>